<template>
  <div class="set-up-container">
    <div class="set-up-main">
      <div class="set-up-item avatar">
        <div class="label">头像</div>
        <div class="value">
          <el-upload
            class="avatar-uploader"
            action
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="userInfo" :src="userInfo && imgUrl + userInfo.headimgurl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>

          <span class="iconfont icongengduo"></span>
        </div>
      </div>
      <div class="set-up-item" @click="editUserRealShow = true">
        <div class="label">昵称</div>
        <div class="value">
          {{ userInfo.userreal }}
          <span class="iconfont icongengduo"></span>
        </div>
      </div>
      <!-- <div class="set-up-item">
        <div class="label">性别</div>
        <div class="value">
          女
          <span class="iconfont icongengduo"></span>
        </div>
      </div>-->
      <div class="set-up-item">
        <div class="label">电话</div>
        <div class="value">
          {{ userInfo.telephone }}
          <span class="iconfont icongengduo"></span>
        </div>
      </div>
      <div class="set-up-item" @click="goTo('/forgetPwd')">
        <div class="label">修改密码</div>
        <div class="value">
          <span class="iconfont icongengduo"></span>
        </div>
      </div>
      <div class="set-up-item" @click="goTo('/about')">
        <div class="label">关于我们</div>
        <div class="value">
          <span class="iconfont icongengduo"></span>
        </div>
      </div>
    </div>

    <!-- 修改昵称弹窗 -->
    <van-dialog
      v-model="editUserRealShow"
      title="修改昵称"
      show-cancel-button
      @open="editUserRealOpen"
      :beforeClose="beforeClose"
    >
      <van-field v-model="userReal" label="昵称" />
    </van-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      editUserRealShow: false,
      userReal: null
    };
  },
  computed: {
    imgUrl() {
      return this.$imgUrl;
    },
    userInfo() {
      return this.$store.state.userData;
    }
  },
  created() {},
  methods: {
    editUserRealOpen() {
      this.userReal = this.userInfo.userreal;
    },
    async beforeClose(action, done) {
      if (action === "cancel") {
        done();
      } else {
        if (!this.userReal) {
          this.$toast('请输入昵称')
          done(false)
          return
        }
        const result = await this.$fetchPost("/home/member/modify", {
          telephone: this.userInfo.telephone,
          userreal: this.userReal,
          headimgurl: this.userInfo.headimgurl
        });
        if (result.success) {
          const userInfo = this.userInfo;
          userInfo.userreal = this.userReal
          this.$store.commit("changeUserData", userInfo);
        }
        done()
      }
    },
    goTo(path) {
      this.$router.push(path);
    },
    async beforeAvatarUpload(file) {
      const formData = new FormData();
      formData.append("file", file);
      const res = await this.$fetchPost("/home/file/upload", formData);
      const result = await this.$fetchPost("/home/member/modify", {
        telephone: this.userInfo.telephone,
        userreal: this.userInfo.userreal,
        headimgurl: res.info
      });
      if (result.success) {
        const userInfo = this.userInfo;
        userInfo.headimgurl = res.info;
        this.$store.commit("changeUserData", userInfo);
      }
      return false;
    }
  }
};
</script>

<style lang="scss">
.set-up-container {
  .set-up-main {
    margin-top: 50px;
    box-shadow: 0 0 10px #888;
    .set-up-item {
      display: flex;
      border-bottom: 1px solid #ebebeb;
      padding: 20px 30px;
      box-sizing: border-box;
      &.avatar {
        height: 160px;
        line-height: 160px;
        .label {
          line-height: 120px;
        }
        .value {
          .iconfont {
            vertical-align: top;
            font-size: 30px;
          }
        }
      }
      .label {
        width: 150px;
        font-size: 30px;
        font-weight: 600;
      }
      .value {
        flex: 1;
        text-align: right;
        color: #949494;
        .avatar-uploader {
          width: 120px;
          height: 120px;
          display: inline-block;
          .avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            border: 1px solid #65a3bf;
          }
        }
        .iconfont {
          margin-left: 30px;
        }
      }
    }
  }
}
</style>